// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html
//游戏数据
let cakeRoom = cc.Class({
    properties:{
       // test:true,
       //本地蛋糕数据
       cakeList:[],
       maxCount:12,


       //用户数据相关
        // token 
       token:null,
       // appId
       appId:null,
       // 账号Id
       accId:null,
       // 当前能量值
       saveGold:null,
       // 最高物品等级
       saveTopLevel:null,
       // 最后一次统计用户拥有物品收益的时间
       saveStaticsTime:null,
       // 存储用户所持有物品的数量
       coalesceItems:null,
       // 记录购买当前等级物品需要的能量，每一次购买都会进行一次价格增幅
       itemGold:null,



        // 用户昵称
       accName:null,
        // 应用账户可提现金额
       accMoney:0,
      // 应用账户拥有金币
       accGold:0,
        // 应用账户当前连续签到次数
       accContinuousSign:0,
        // 用户最后一次签到时间
       accLastSignDate:null,
       // 	用户最后一次提现时间
       accLastExtractMoneyDate:null,
        // 	用户的推广码
       invicationCode:null,
        // 	用户的上级推广码
       parentInvicationCode:null,
        // 	应用账户已经被提现的金额数
       accExtractMoney:0,
       isDebug:true,

       cakePosList:[],

       isCanSign:true,
       localTxList:[],
        //刮刮卡未使用次数
       unuseCount:0,

       //金币开红包的最小金币数
       hbMinCoin:3000,
    },


    init(){
       // cc.log("cake room init");
       this.cakeList = [];
       this.cakePosList = [];
       this.maxCount = 12;
        this.isDebug = false;
        this.localTxList = [1,3,5,20,50,100,50,50];
       //测试
    //    for(let i = 0 ; i < this.maxCount;++i)
    //    {
    //        let data = {}
    //        if(i % 3 == 0)
    //        {
    //            data.info = {
    //                pos:i,
    //                rank:1+i,
    //                count:this.maxCount-i
    //            }
               
    //        }
    //        this.cakeList.push(data);
    //    }
    },


/*
data":{"id":1,"appId":"appid_dg","accId":"dGVzdE9wZW5JZA==","saveGold":1000,"saveItem":null,
"saveTopLevel":1,"saveStatisticsTime":1581832601366,
"coalesceItems":{"f30":0,"f10":0,"f32":0,"f31":0,"f12":0,"f34":0,"f11":0,"f33":0,"f14":0,"f36":0,"f13":0,"f35":0,"f16":0,"f1":0,"f15":0,"f37":0,"f2":0,"f18":0,"f3":0,"f17":0,"f4":0,"f5":0,"f19":0,"f6":0,"f7":0,"f8":0,"f9":0,"f21":0,"f20":0,"f23":0,"f22":0,"f25":0,"f24":0,"f27":0,"f26":0,"f29":0,"f28":0},
"itemGold":{"f30":96258282207,"f10":755071,"f32":311876834350,"f31":173264907972,"f12":2446431,"f34":1010480943295,"f11":1359128,"f33":561378301831,"f14":7926437,"f36":3273958256277,"f13":4403576,"f35":1818865697932,"f16":25681657,"f1":100,"f15":14267587,"f37":5893124861299,"f2":960,"f18":83208568,"f3":7488,"f17":46226982,"f4":22200,"f5":39960,"f19":149775422,"f6":71928,"f7":129470,"f8":233047,"f9":419484,"f21":485272366,"f20":269595759,"f23":1572282467,"f22":873490259,"f25":5094195192,"f24":2830108440,"f27":16505192422,"f26":9169551346,"f29":53476823448,"f28":29709346360}}} 

*/
    initByUserData(data){
        if(!data)
            return ;
        this.appId = data.appId;
        cc.sys.localStorage.setItem("appId",this.appId)
        //window.localStorage.setItem("appId",this.appId);
        this.accId = data.accId;
        this.saveGold = data.saveGold;
        this.saveItem = data.saveItem;
        this.saveTopLevel = data.saveTopLevel;
        this.saveStaticsTime = data.saveStaticsTime;
        this.coalesceItems = data.coalesceItems;
        this.itemGold = data.itemGold;
        for(let key in this.coalesceItems)
        {
            this.updateOneCake(key,this.itemGold[key],this.coalesceItems[key])
            
        }
        cc.tg.eventCenter.dispatchEvent("updateCake");
    },


    initAccInfo(data){
        if(!data)
            return ;
        this.accName = data.accName;
        this.accGold = data.accGold;
        this.accMoney = data.accMoney;
        this.accExtractMoney = data.accExtractMoney;
        this.accContinuousSign = data.accContinuousSign;
        this.accLastSignDate = data.accLastSignDate;
        this.accLastExtractMoneyDate = data.accLastExtractMoneyDate;
        // this.invicationCode = data.invicationCode;
        // this.parentInvicationCode = data.invicationCode;
        cc.tg.eventCenter.dispatchEvent("updateCoin",data.accGold);
        cc.tg.eventCenter.dispatchEvent("updateTxCoin",data.accMoney)
    },
    //快速买一个蛋糕
    buyOneItem(){
        let index = this.getIndex();
        if(index < 0 || index >= this.maxCount)
        {
            cc.log("购买物品失败");
            return ;
        }
        cc.tg.gameLogic.toBuyItem(this.cakeList[0].itemCode)
    },

    onBuyItemSucess(data,itemCode){
        let itemGold = data.itemGold[itemCode];
        let itemCount = data.coalesceItems[itemCode];
        this.updateOneCake(itemCode,itemGold,itemCount);
        let index = this.getIndex(itemCode);
        if(index >= 0 )
        {

            this.cakePosList[index] = itemCode;
            let cake = this.createCakeData(index,itemCode,1);
            cc.tg.eventCenter.dispatchEvent("onBuyCake",cake);
        }
        else
        {
            cc.log("get index error");
        }
    },


    updateOneCake(code,gold,count){
      
        let data = {
            itemCode:code,
            itemGold:gold,
            itemCount:count,
           
        }
        let index = code.substr(1)-1;
        this.cakeList[index] = data; 
    },
    // 获取一个蛋糕pos的index
    getIndex(itemCode = null){
       let index = 0;
       for(let i = 0 ; i < this.maxCount ;++i)
       {
         let pos = this.cakePosList[i]
        if(pos == null)
           {    
                index =  i ;
              
                break;
           }    
       }
       if(itemCode != null)
       {
          this.cakePosList[index] = itemCode;
       }
      
       return index ;
    },
    // 该位置上是否有蛋糕
    // -1 没有 0 有但是不相同, 1 有并且相同
    // tpos 要移动到的pos
    // spos 起始pos
    // rank 移动蛋糕的rank
    updateLevel(tPos,sPos){
        
        let ret = {
            code:-1,
            dataList:[],
            posList:[sPos,tPos],
        };
       // cc.error("tPos" + tPos + " Spos"  + sPos);
        let sData =  this.getCakeDataByPos(sPos);
        let tData =  this.getCakeDataByPos(tPos);
        ret.dataList = [sData,tData];
        // if(sPos == tPos)
        // {
        //     tData = sData;
        // }
        // let pData = {
        //     itemCode:null,
        // }
        //
        // if(!tData)
        // {
        //     ret.code = -1;
        //     sData.info.pos = tPos;
        //     this.cakeList[tPos].info = sData.info;
        //     this.cakeList[sPos].info = null;
           
        //   //  dataList.push(sData);
        // }
        // else if(tData.info.pos == sData.info.pos)
        // {
        //     ret.code = -1;
            
        // }
        // else if(sData.info.rank == tData.info.rank)
        // {
        //     ret.code = 1;
        //     this.toUpdateLevel(sData,tData);
           
        // }
        // else if(sData.info.rank != tData.info.rank)
        // {
        //     ret.code = 0;
        //   //  cc.log(sData.info.pos ,tData.info.pos);
        //     this.toChange(sData,tData);
        //     //ret.dataList.push(tData);
        // }
        if(tData.rank < 0)
        {
            // tPos 上面没有物品
            ret.code = -1;
           
            
            this.cakePosList[tData.pos] = this.cakePosList[sData.pos];
            this.cakePosList[sData.pos] = null;
            sData.pos = tData.pos;
            cc.tg.eventCenter.dispatchEvent("updateRet",ret);
        }
        else if(sData.pos == tData.pos){
            //位置相同
            ret.code = -1;
            cc.tg.eventCenter.dispatchEvent("updateRet",ret);
        }
        else if(sData.rank == tData.rank)
        {
            // 升级
          //  ret.code = 1
            let level = sData.rank +2;
            cc.tg.gameLogic.toUpdateLevel(level,sPos,tPos);
        }
        else if(sData.rank != tData.rank)
        {
            // 交换位置
            ret.code = 0;
            let temp = this.cakePosList[tData.pos];
            this.cakePosList[tData.pos] = this.cakePosList[sData.pos];
            this.cakePosList[sData.pos] = temp;
            
            sData.pos = tPos;
            tData.pos = sPos;
            cc.tg.eventCenter.dispatchEvent("updateRet",ret);
        }

       
    },

    //合并成功
    onUpdateSucess(data,sPos,tPos){
        let itemCode = this.cakePosList[tPos];
      //  let itemCount = data[itemCode].itemCount;
        let tCake = this.createCakeData(tPos,itemCode,1);
        let sCake = this.createCakeData(sPos,itemCode,1);
        tCake.rank++;
        // cc.tg.eventCenter.dispatchEvent("onBuyCake",cake);
         let ret = {
            code:1,
            dataList:[sCake,tCake],
            posList:[sPos,tPos],
        };
        cc.tg.eventCenter.dispatchEvent("updateRet",ret);
         this.cakePosList[tPos] = this.cakeList[tCake.rank].itemCode;
         this.cakePosList[sPos] = null;
    },

    onResolveSucess(data,pos){
        this.cakePosList[pos] = null;
        cc.tg.eventCenter.dispatchEvent("onResolveSucess")
    },
    getCakeDataByPos(pos){
        let itemCode = this.cakePosList[pos];
        let itemCount = null
        if(itemCode)
        {
            itemCount = this.getItemCount(itemCode);
        }
   
        return this.createCakeData(pos,itemCode,itemCount);
    },

    getCakeDataByItemCode(itemCode){
        if(!itemCode)
            return null;
        let pos = null;
        let itemCount = this.getItemCount(itemCode);
        return this.createCakeData(pos,itemCode,itemCount);
    },

    createCakeData(pos,itemCode,itemCount){
        let rank = itemCode == null ? -1 : itemCode.substr(1)-1
        let cake = {
            pos:pos,
            rank:rank,
            count:itemCount
        }
        return cake;
    },

    getItemCount(itemCode){
        if(!itemCode)
            return null;
        return  this.cakeList[itemCode.substr(-1)-1].itemCount;
    }



});
module.exports = cakeRoom;